package cn.xuetian.activiti.controller;

import cn.xuetian.activiti.service.ITestService;
import org.activiti.engine.*;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zhaosen
 * @date 4/1/2020
 */
@RestController
@RequestMapping("/test")
public class TestController {

    @Autowired
    private ITestService testService;


    @Autowired
    private ProcessEngine processEngine;

    @Autowired
    private RuntimeService runtimeService;

    @Autowired
    private TaskService taskService;

    @Autowired
    private RepositoryService repositoryService;

    @Autowired
    private HistoryService historyService;


    @GetMapping("/test1")
    public String test1() {

        return testService.method1();
    }


    @GetMapping("/deploy")
    public String deploy() {

        Deployment deploy = repositoryService.createDeployment()//创建一个部署的对象
                .name("请假流程定义")//创建流程名称
                .addClasspathResource("processes/simple.bpmn")//指定zip完成部署
                .deploy();

        long count = repositoryService.createProcessDefinitionQuery().count();

        System.out.println("部署id" + deploy.getId());
        System.out.println("部署名称" + deploy.getName());
        System.out.println("部署key" + deploy.getKey());

        System.out.println("部署名称" + count);

        return null;
    }


    @GetMapping("/start")
    public Void start(String key) {

        Map<String, Object> variables = new HashMap<>();
        variables.put("user", "1");

        //可根据id、key、message启动流程
        ProcessInstance processInstance = runtimeService.startProcessInstanceByKey(key,variables);
        System.out.println(processInstance.getName());
        System.out.println(processInstance.getProcessDefinitionId());
        System.out.println(processInstance.getId());


        return null;
    }


    @GetMapping("/queryTask")
    public Void queryTask(String assignee) {

        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();

        TaskService taskService = processEngine.getTaskService();

        //根据assignee(代理人)查询任务
        List<Task> tasks = taskService.createTaskQuery().taskAssignee(assignee).list();

        int size = tasks.size();
        for (int i = 0; i < size; i++) {
            Task task = tasks.get(i);
        }

        //获取最新一条
        for (Task task : tasks) {
            System.out.println("taskId:" + task.getId() +
                    ",taskName:" + task.getName() +
                    ",assignee:" + task.getAssignee() +
                    ",createTime:" + task.getCreateTime());
        }

        return null;
    }


    /**
     * 办理任务
     */
    @GetMapping("/handleTask")
    public void handleTask(String taskId) {

        //根据上一步生成的taskId执行任务
        taskService.complete(taskId);
    }
}
